/**
 * 
 */
package action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import myfamily.model.GenericPerson;
import myfamily.model.RegisteredUser;

import org.expressme.openid.Authentication;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author JATIN
 *
 */
public class EditProfile extends ActionSupport{
	private List<String> operations;
	private List<String> familymembers;
	private String treeString;
	private String firstName;
	private String middleName;
	private String lastName;
	private String phoneNo;
	private String authId;
	private String submit;

	public String execute(){

		try{
			Map session = ActionContext.getContext().getSession();
			Authentication auth = new Authentication();
			auth = (Authentication)session.get("authentication");
			setAuthId(auth.getIdentity().split("=")[1]);
			RegisteredUser ru = RegisteredUser.login(getAuthId());
			GenericPerson gp = (GenericPerson)session.get("selectedPerson");
			
			operations = new ArrayList<String>();
			operations.add("Add Child");
			operations.add("Add Parents");
			operations.add("Add Partner");
			operations.add("Add Sibling");
			operations.add("Delete Member");
			operations.add("Edit Details");
			familymembers = new ArrayList<String>();
			
			
			if(submit==null){

				System.out.println(gp.getPersonName().toString());
				System.out.println(gp.getPersonName().getFirstName()+"  "+ gp.getPersonName().getLastName());
				setFirstName(gp.getPersonName().getFirstName());
				setMiddleName(gp.getPersonName().getMiddleName());
				setLastName(gp.getPersonName().getLastName());
				setPhoneNo(gp.getContactDetails().getPhone());

				return "showEditProfileForm";

			}else{
				
				System.out.println(getFirstName()+" "+getMiddleName()+" "+ getLastName()+" "+ getPhoneNo());

            gp.editProfile(getFirstName(),getMiddleName(), getLastName(), getPhoneNo());
            	
            familymembers.clear();
			ArrayList<GenericPerson> fm = ru.identifyExtremeAncestors();
			for(GenericPerson g: fm){
				familymembers.add(g.getPersonName().toString());
				
			}
			
			setTreeString(ru.printTree().toString());
			
				return "showUpdatedProfile";
			}
			
			}catch(Exception e){
				e.printStackTrace();
			}


			return "showLogin";

		}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getMiddleName() {
		return middleName;
	}

	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getPhoneNo() {
		return phoneNo;
	}

	public void setPhoneNo(String phoneNo) {
		this.phoneNo = phoneNo;
	}

	public String getAuthId() {
		return authId;
	}

	public void setAuthId(String authId) {
		this.authId = authId;
	}

	public String getSubmit() {
		return submit;
	}

	public void setSubmit(String submit) {
		this.submit = submit;
	}

	public List<String> getOperations() {
		return operations;
	}

	public void setOperations(List<String> operations) {
		this.operations = operations;
	}

	public List<String> getFamilymembers() {
		return familymembers;
	}

	public void setFamilymembers(List<String> familymembers) {
		this.familymembers = familymembers;
	}

	public String getTreeString() {
		return treeString;
	}

	public void setTreeString(String treeString) {
		this.treeString = treeString;
	}

	

	}
